//
//  MineMessageController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/7/6.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "MineMessageController.h"
#import "MineMessageCell.h"
#import "MessageModel.h"
#import "MesInfoController.h"
@interface MineMessageController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * msgTableVeiw;
@property(nonatomic,strong)NSMutableArray * mesArray;
@property(nonatomic,strong)MessageModel * messageModel;
@end

@implementation MineMessageController
{
    int _pageNo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNo = 0;
    _mesArray = [NSMutableArray array];
    self.title = @"我的消息";
    [self createMsgTableView];
    [self requestMessage];
}
-(void)createMsgTableView
{
    _msgTableVeiw=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStylePlain];
    _msgTableVeiw.delegate=self;
    _msgTableVeiw.dataSource=self;
    _msgTableVeiw.showsVerticalScrollIndicator=NO;
    _msgTableVeiw.showsHorizontalScrollIndicator=NO;
    _msgTableVeiw.userInteractionEnabled= YES ;
    
    _msgTableVeiw.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _msgTableVeiw.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_msgTableVeiw];
    
    __weak typeof(self) weakSelf = self;
    //默认block方法：设置下拉刷新
    _msgTableVeiw.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    // 马上进入刷新状态
    [_msgTableVeiw.mj_header beginRefreshing];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    _msgTableVeiw.mj_header.automaticallyChangeAlpha = YES;
    
    //默认block方法：设置上拉加载更多
    _msgTableVeiw.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];

}

-(void)loadData
{
    _pageNo = 0;
    [self requestMessage];
}

-(void)loadNewData
{
    _pageNo++;
    [self requestMessage];
}

-(void)endRefresh{
    [_msgTableVeiw.mj_header endRefreshing];
    [_msgTableVeiw.mj_footer endRefreshing];
}
#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        MineMessageCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"MineMessageCell" owner:nil options:nil] lastObject];
       _messageModel= self.mesArray[indexPath.row];
    cell.titleLab.text = _messageModel.subject;
    cell.typeLab.text = _messageModel.typeName;
    cell.timeLab.text = _messageModel.createTime;

        return cell;
    
}

#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       return 69;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _messageModel = self.mesArray[indexPath.row];
    MesInfoController *vc = [[MesInfoController alloc] init];
    vc.mid = [NSString stringWithFormat:@"%d",_messageModel.ID];
    [self.navigationController pushViewController:vc animated:YES];
    

}

-(void)requestMessage
{
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid],
                             @"page":[NSString stringWithFormat:@"%d",_pageNo]
                            };
    [NetWorkManager requestWithType:1 withUrlString:@"/message/getMessageListByUserid.do" withParaments:params withSuccessBlock:^(id object) {
        [self endRefresh];
        if ([object[@"status"]intValue]==1 ) {
            if (_pageNo == 0) { // 说明是在重新请求数据.
                [self.mesArray removeAllObjects];
            }
            NSArray * nowArray = [MessageModel mj_objectArrayWithKeyValuesArray:object[@"appMessageList"]];
            [self.mesArray addObjectsFromArray: nowArray];
            [_msgTableVeiw reloadData];
            
            if (self.mesArray.count < 10) {//本次接口获得的数据列表数<10
                _msgTableVeiw.mj_footer.state = MJRefreshStateNoMoreData;;//显示无更多数据
            }

        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
